package com.von.util;

import java.io.*;
import java.util.Base64;

/**
 * @author xiaosai
 * @version 1.0
 * @date 2021/9/9 13:54:29
 * @describe
 * @className
 */

public class PdfUtils {
    /**
     * @param base64Content
     * @param filePath
     * base64字符串转pdf
     */
    public static void base64StringToPdf(String base64Content, String filePath) {
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            // base64编码内容转换为字节数组
            byte[] bytes = Base64.getDecoder().decode(base64Content);
            ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
            bis = new BufferedInputStream(byteInputStream);
            File file = new File(filePath);
            File path = file.getParentFile();
            if (!path.exists()) {
                path.mkdirs();
            }
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            byte[] buffer = new byte[1024];
            int length = bis.read(buffer);
            while (length != -1) {
                bos.write(buffer, 0, length);
                length = bis.read(buffer);
            }
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bos.close();
                fos.close();
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
